package org.cn.osiris.basic.controller;

import org.cn.osiris.basic.dto.LoginDto;
import org.cn.osiris.basic.exception.BusinessException;
import org.cn.osiris.basic.util.AjaxResult;
import org.cn.osiris.user.service.ILogininfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ILogininfoService logininfoService;

    @PostMapping("/account")
    public AjaxResult accountLogin(@RequestBody LoginDto loginDto){

        try {
            Map<String,Object> map = logininfoService.accountLogin(loginDto);
            //返回map集合的json数据
            return  AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            //抛出校验异常
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试");
        }
    }

    /**微信登录接口*/
    @GetMapping("/wechat/{code}")
    public AjaxResult webchatLogin(@PathVariable("code")  String code){
        try {
            return logininfoService.wechatLogin(code);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试");
        }
    }
    /**
     * 微信绑定接口：获取手机验证码
     * @return
     * map【map只能是一种，其他类型需要手动转换】  和    dto【为每一个数据定义类型】，需要定义一个dto
     */
    @PostMapping("/binderSmsCode")
    public AjaxResult binderSmsCode(@RequestBody Map<String,String> map){
        try {
            return logininfoService.wechatBinder(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,"发送失败，" + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙!!!");
        }
    }

    /**
     * 微信绑定接口
     * @param map
     * @return
     * map【map只能是一种，其他类型需要手动转换】  和    dto【为每一个数据定义类型】，需要定义一个dto
     */
    @PostMapping("/wechat/binder")
    public AjaxResult wechatBinder(@RequestBody Map<String,String> map){
        try {
            return logininfoService.wechatBinder(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试!!!");
        }
    }
}
